﻿using System.Collections.Generic;
using System.Windows.Forms;
using FluentNH_DO.Entities;

namespace FormsClient
{
    public partial class Form1 : Form
    {
        private FluentNH_DAL.DataProvider _dataProvider;

        public Form1()
        {
            InitializeComponent();
            InitializeListView();
            LoadListView2();
        }

        private void InitializeListView()
        {
            // Set the view to show details.
            listView2.View = View.Details;

            // Allow the user to edit item text.
            listView2.LabelEdit = false;

            // Allow the user to rearrange columns.
            listView2.AllowColumnReorder = false;

            // Select the item and subitems when selection is made.
            //listView2.FullRowSelect = true;

            // Display grid lines.
            listView2.GridLines = true;

            // Sort the items in the list in ascending order.
            //listView2.Sorting = SortOrder.Ascending;

            // Attach Subitems to the ListView
            listView2.Columns.Add("Name", 150, HorizontalAlignment.Left);
            //listView2.Columns.Add("ID", 70, HorizontalAlignment.Left);
            //listView2.Columns.Add("Price", 70, HorizontalAlignment.Left);
            //listView2.Columns.Add("Publi-Date", 100, HorizontalAlignment.Left);

            // The ListViewItemSorter property allows you to specify the
            // object that performs the sorting of items in the ListView.
            // You can use the ListViewItemSorter property in combination
            // with the Sort method to perform custom sorting.
            //_lvwItemComparer = new ListViewItemComparer();
            //this.listView1.ListViewItemSorter = _lvwItemComparer;
        }

        private void LoadListView2()
        {
            _dataProvider = new FluentNH_DAL.DataProvider();
            IList<Contact> contacts = _dataProvider.GetContacts();

            listView2.Items.Clear();

            foreach (Contact c in contacts)
            {
                ListViewItem lvi = new ListViewItem(c.Name);
                listView2.Items.Add(lvi);// = _contactService.GetContacts();
            }
            
            
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Contact newContact = new Contact {Name = textBox1.Text};
            _dataProvider = new FluentNH_DAL.DataProvider();
            _dataProvider.AddContact(newContact);
            LoadListView2();
        }

        private void listView2_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem lvi = e.Item;
            Contact c = new Contact {Name = lvi.Text};
            _dataProvider = new FluentNH_DAL.DataProvider();
            _dataProvider.GetContactInfo(c);
        }




    }
}
